/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Settings;

import com.thoughtworks.xstream.*;
import java.io.*;
import org.lwjgl.*;

/**
 *
 * @author RikuXan
 */
public class Settings
{
    //graphics (die static Variablen werden nicht in die XML-Datei übernommen, daher ist eine Nichst-static Version nötig)
    public int _wWidth;
    public static int wWidth = 800;
    public int _wHeight;
    public static int wHeight = 600;
    public int _wFrequency;
    public static int wFrequency = 59;
    public boolean _wVSync;
    public static boolean wVSync = false;
    public boolean _wFullscreen;
    public static boolean wFullscreen = false;
    public int _wTilesPerWidth;
    public static int wTilesPerWidth = 25;
    public int _wbmpSmallest;
    public static int wbmpSmallest = 16;
    public int _wbmpBiggest;
    public static int wbmpBiggest = 64;
    public float _moveStd;
    public static float moveStd = 0.1f;
    public int _startLength;
    public static int startLength = 4;
    public int _scorePerFood;
    public static int scorePerFood = 10;
    public int _scorePerMove;
    public static int scorePerMove = 0;
    public int _endscore;
    public static int endscore = 100;
    public int _levelOnTheRun;
    public static int levelOnTheRun = 1;
    public int _levelStd;
    public static int levelStd = 1;
    public String _gameplay;
    public static String gameplay = "Standard";
    public int _highScore;
    public static int highScore = 0;
    public int _maxLevel;
    public static int maxLevel;

    //general
    public boolean _gameRuns;
    public static boolean gameRuns = true;

    //xml-stuff
    private static String xml;

    public Settings()
    {
       // loadSettings();
    }

    //Settings aus XML-Datei laden
    public static void loadSettings()
    {
        //Den String, aus dem das Settingsobjekt erstellt werden soll leereen
        xml = "";

        //Settings-Datei öffnen
        File in = new File( "Settings/Settings.xml" );

        //Überprüfen, ob die Datei vorhanden ist und ob man sie auslesen kann
            //Wenn ja, dann auslesen
            //Wenn nicht, dann Fehlermeldung ausgeben
        if( in.exists() && in.canRead() )
        {
            try
            {
                //einen Reader erstellen, der die Datei ausliest
                BufferedReader dataIn = new BufferedReader(new FileReader(in ) );

                String line = "1";
                Settings settings = new Settings();

                //bis zum Ende der Datei zeilenweise auslesen
                while((line = dataIn.readLine() ) != null )
                {
                    xml += line;
                }

                //Neues XStream-Objekt erstellen
                XStream xstream = new XStream();

                //Neues Settings-Objekt aus dem XML-String erstellen
                settings = (Settings)xstream.fromXML( xml );

                wFrequency = settings._wFrequency;
                wFullscreen = settings._wFullscreen;
                wHeight = settings._wHeight;
                wVSync = settings._wVSync;
                wWidth = settings._wWidth;
                gameRuns = settings._gameRuns;
                wTilesPerWidth = settings._wTilesPerWidth;
                wbmpBiggest = settings._wbmpBiggest;
                wbmpSmallest = settings._wbmpSmallest;
                scorePerFood = settings._scorePerFood;
                scorePerMove = settings._scorePerMove;
                endscore = settings._endscore;
                levelOnTheRun = settings._levelOnTheRun;
                levelStd = settings._levelStd;
                gameplay = settings._gameplay;
                highScore = settings._highScore;
                maxLevel = settings._maxLevel;

                //Reader wieder schliesen
                dataIn.close();
            }
            catch(IOException e)
            {
                Sys.alert("Error" , "Konnte Einstellungen nicht laden. Unbekannter Fehler!");
                //Fehler beim lesen aufgetreten
            }
        }
        else
        {
            Sys.alert("Error" , "Konnte Einstellungen nicht laden. Datei nicht vorhanden, oder nicht lesbar!");
            //Fehler; Datei nicht vorhanden oder nicht lesbar
        }
    }


    //Settings in XML-Datei speichern
    public static void saveSettings()
    {
        //Neues XStream-Objekt erstellen
        XStream xstream = new XStream();

        Settings settings = new Settings();

        settings._wFrequency = wFrequency;
        settings._gameRuns = gameRuns;
        settings._wFullscreen = wFullscreen;
        settings._wHeight = wHeight;
        settings._wVSync = wVSync;
        settings._wWidth = wWidth;
        settings._wTilesPerWidth = wTilesPerWidth;
        settings._wbmpBiggest = wbmpBiggest;
        settings._wbmpSmallest = wbmpSmallest;
        settings._scorePerFood = scorePerFood;
        settings._scorePerMove = scorePerMove;
        settings._endscore = endscore;
        settings._levelOnTheRun = levelOnTheRun;
        settings._levelStd = levelStd;
        settings._gameplay = gameplay;
        settings._highScore = highScore;
        settings._maxLevel = maxLevel;

        //Das Settings-Objekt als XML-Code in den String xml schreiben
        xml = xstream.toXML( settings );
        //Settings-Datei öffnen
        File out = new File("Settings/Settings.xml");

        try
        {
            //einen Writer erstellen, der die Datei ausliest
            BufferedWriter dataOut = new BufferedWriter(new FileWriter(out) );

            //Settings in die Datei schreiben
            dataOut.write(xml);
            
            //Writer wieder schliesen
            dataOut.close();
        }
        catch( IOException e )
        {
            Sys.alert("Error" , "Konnte Einstellungen nicht speichern. Unbekannter Fehler!");
                //Fehler beim schreiben aufgetreten
        }
    }
}
